// src/store/modules/user.js
import { defineStore } from 'pinia'
import { getToken, setToken, removeToken } from '@/utils/auth'

const useUserStore = defineStore('user', {
  state: () => ({
    token: getToken() || '',
    userInfo: {
      username: ''
    }
  }),
  actions: {
    setUserInfo(user) {
      this.userInfo = {
        username: user.username
      }
    },
    setToken(token) {
      this.token = token
      setToken(token)
    },
    removeToken() {
      this.token = ''
      removeToken()
    },
    logOut() {
      return new Promise((resolve) => {
        this.token = ''
        this.userInfo = ''
        localStorage.removeItem('ZR-Token')  // 如果你 token 存在 localStorage
        resolve()
      })
    }

  }
})

export default useUserStore
